package bgpv1

import (
	"fmt"
	"topobuild/utils"

	"github.com/xuri/excelize/v2"
)

/*
设备名称	邻居设备标识符	RouteMap名称	RouteMap方向	本端AS域
R2001	192.168.201.174	RM-3	in	10
R2002	172.30.254.93	RM-2	out	20
*/

type TableBGPv1Rm struct {
	DevName string `json:"dev_name"`
	NeiId   string `json:"nei_id"`
	RmName  string `json:"rm_name"`
	RmDir   string `json:"rm_dir"`
	As      int    `json:"as"`
}

// 将TableBGPv1Rm中的数据,写入到指定xlsx文件的unicast_route表中
func WriteRmXlsx(filename string, rms []TableBGPv1Rm) {
	// 如果filename已经存在,则直接打开,否则创建一个新文件
	utils.CheckFile(filename)
	xlsx, err := excelize.OpenFile(filename)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer xlsx.Close()
	// Create a new sheet.
	index, err := xlsx.NewSheet("unicast_route")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Set value of a cell.
	xlsx.SetCellValue("unicast_route", "A1", "设备名称")
	xlsx.SetCellValue("unicast_route", "B1", "邻居设备标识符")
	xlsx.SetCellValue("unicast_route", "C1", "RouteMap名称")
	xlsx.SetCellValue("unicast_route", "D1", "RouteMap方向")
	xlsx.SetCellValue("unicast_route", "E1", "本端AS域")
	// 设置单元格样式
	// style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center"}}`)
	// xlsx.SetCellStyle("dev", "A1", "G1", style)
	// 设置列宽
	// xlsx.SetColWidth("dev", "A", "G", 20)
	// 设置行高
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	fmt.Printf("len(rms): %v\n", len(rms))
	for i, rm := range rms {
		// fmt.Printf("rm: %v\n", rm)
		r := i + 2
		xlsx.SetCellValue("unicast_route", fmt.Sprintf("A%v", r), rm.DevName)
		xlsx.SetCellValue("unicast_route", fmt.Sprintf("B%v", r), rm.NeiId)
		xlsx.SetCellValue("unicast_route", fmt.Sprintf("C%v", r), rm.RmName)
		xlsx.SetCellValue("unicast_route", fmt.Sprintf("D%v", r), rm.RmDir)
		xlsx.SetCellValue("unicast_route", fmt.Sprintf("E%v", r), rm.As)
	}
	xlsx.Save()
}
